import {
    post,
    get,
    put,
    Delete,
    form
} from "@/axios/http.js";
import { setCookie,removeCookie,getCookie } from "@/utils/auth";
import {
    basicApi
} from "@/api/index.js"
//模块数据
export default {
    namespaced: true,
    state: {
        articleOrPost:{}, // 打开文章或者帖子时暂存用
        articleClassificationList: [], // 文章分类列表
        articleFriendList: [], // 好友文章列表
        articleOrDraftList: [], // 我的文章/草稿列表/包含好的的文章
    },
    actions: {
        // 文章——上/下一篇文章
        lastAndNextArticle({
            commit
        }, params) {
            return post(basicApi.article.lastAndNextArticle, params).then(res => {
                if(res.data.code == 200){
                    return res.data.result
                }else{
                    this._vm.$message.error(res.data.msg)
                }
            });
        },
        // 文章列表/草稿箱列表
        schoolArticleList({
            commit
        }, params) {
            return post(basicApi.article.schoolArticleList, params).then(res => {
                return res
            });
        },
        // 写文章、从草稿到发布
        schoolArticleIssue({
            commit
        }, params) {
            return post(basicApi.article.schoolArticleIssue, params).then(res => {
                return res
            });
        },
        // 文章转发
        articleTransmit({
            commit
        }, params) {
            return post(basicApi.article.articleTransmit, params).then(res => {
                return res
            });
        },
        // 空间设置--更新
        articleSetting({
            commit
        }, params) {
            return put(basicApi.article.articleSetting, params).then(res => {
                return res
            });
        },
        // 空间设置--查询
        articleGetSetting({
            commit
        }, params) {
            return get(basicApi.article.articleGetSetting, params).then(res => {
                return res
            });
        },
        // 文章权限设置
        jurisdiction({
            commit
        }, params) {
            return put(basicApi.article.jurisdiction, params).then(res => {
                return res
            });
        },
        // 文章批量删除
        articlesDel({
            commit
        }, params) {
            return put(basicApi.article.articlesDel, params).then(res => {
                return res
            });
        },
        // 文章批量修改分类
        classify({
            commit
        }, params) {
            return put(basicApi.article.classify, params).then(res => {
                return res
            });
        },
        // 文章管理分类删除
        del({
            commit
        }, params) {
            return Delete(basicApi.article.del, params).then(res => {
                return res
            });
        },
        // 文章权限设置
        articleJurisdictionUpdate({
            commit
        }, params) {
            return get(basicApi.article.articleJurisdictionUpdate, params).then(res => {
                return res
            });
        },
        // 学校首页-文章系列
        articleSchool({
            commit
        }, params) {
            return post(basicApi.article.articleSchool, params).then(res => {
                return res
            });
        },
        // 首页-最新文章列表
        articleNewest({
            commit
        }, params) {
            return post(basicApi.article.articleNewest, params).then(res => {
                return res
            });
        },
        // 写文章
        articleIssue({
            commit
        }, params) {
            return post(basicApi.article.articleIssue, params).then(res => {
                return res
            });
        },
        // 好友文章列表
        articleFriendList({
            commit
        }, params) {
            return post(basicApi.article.articleFriendList, params).then(res => {
                if (res.data.code == 200) {
                    commit('INIT_ARTICLEORDRAFTLIST', res.data.result.list)
                    return res
                }
            });
        },
        // 文章日历查询
        articleCalendar({
            commit
        }, params) {
            return get(basicApi.article.articleCalendar, params).then(res => {
                return res
            });
        },
        // 文章删除
        articleClassificationDelete({
            commit
        }, params) {
            return get(basicApi.article.articleClassificationDelete, params).then(res => {
                return res
            });
        },
        // 文章批量修改分类--ok
        articleClassificationMove({
            commit
        }, params) {
            return get(basicApi.article.articleClassificationMove, params).then(res => {
                return res
            });
        },
        // 好友文章内容--ok
        articleFriendContent({
            commit,dispatch
        }, params) {
            if(params.id == null){
                params.id = JSON.parse(getCookie('articleOrPost')).id
            }
            return get(basicApi.article.articleFriendContent, params).then(res => {
                return res
            });
        },
        // 文章管理分类编辑/添加--ok
        articleClassificationSave({
            commit
        }, params) {
            return post(basicApi.article.articleClassificationSave, params).then(res => {
                return res
            });
        },
        // 文章列表/草稿箱列表
        articleList({
            commit
        }, params) {
            return post(basicApi.article.articleList, params).then(res => {
                commit('INIT_ARTICLEORDRAFTLIST', res.data.result.list)
                return res
            });
        },
        //文章管理分类列表
        articleClassificationList({
            commit
        }, params) {
            return get(basicApi.article.articleClassificationList, params).then(res => {
                if (res.data.result) {
                    commit('INITTYPELIST', res.data.result)
                }
                return res
            });
        },
    },
    mutations: {
        INITTYPELIST(state, params) {
            state.articleClassificationList = params
        },
        INIT_ARTICLEORDRAFTLIST(state, params) {
            state.articleOrDraftList = params
        },
        INIT_ARTICLEORPOST(state,params) {
            setCookie('articleOrPost',params)
            state.articleOrPost= params
        }
    },
};